/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Product.java
 *
 * Created on 01-04-2011, 06:12:34
 */

package GuiLayer;

import CtrLayer.CtrSupplier;
import DBLayer.IFDBSupplier;
import GuiLayer.Costumer.*;
import GuiLayer.Product.*;
import ModelLayer.*;

/**
 *
 * @author Sharif
 */
public class Supplier extends javax.swing.JFrame {

    /** Creates new form Product */
    public Supplier() {
        initComponents();
         this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jID = new javax.swing.JLabel();
        jName = new javax.swing.JLabel();
        jAddress = new javax.swing.JLabel();
        jCountry = new javax.swing.JLabel();
        jPhone = new javax.swing.JLabel();
        jEmail = new javax.swing.JLabel();
        jTextID = new javax.swing.JTextField();
        jTextName = new javax.swing.JTextField();
        jTextAddress = new javax.swing.JTextField();
        jTextCountry = new javax.swing.JTextField();
        jTextPhone = new javax.swing.JTextField();
        jTextEmail = new javax.swing.JTextField();
        jButtonInsert = new javax.swing.JButton();
        jButtonUpdate = new javax.swing.JButton();
        jButtonSearch = new javax.swing.JButton();
        jButtonSearch1 = new javax.swing.JButton();
        jButtonSearch2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jID.setText("Id");

        jName.setText("Name");

        jAddress.setText("Address");

        jCountry.setText("Country");

        jPhone.setText("Phone");

        jEmail.setText("Email");

        jTextID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextIDActionPerformed(evt);
            }
        });

        jButtonInsert.setText("Insert New");
        jButtonInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonInsertActionPerformed(evt);
            }
        });

        jButtonUpdate.setText("Update");
        jButtonUpdate.setMaximumSize(new java.awt.Dimension(85, 23));
        jButtonUpdate.setMinimumSize(new java.awt.Dimension(85, 23));
        jButtonUpdate.setPreferredSize(new java.awt.Dimension(85, 23));
        jButtonUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUpdateActionPerformed(evt);
            }
        });

        jButtonSearch.setText("Search");
        jButtonSearch.setMaximumSize(new java.awt.Dimension(85, 23));
        jButtonSearch.setMinimumSize(new java.awt.Dimension(85, 23));
        jButtonSearch.setPreferredSize(new java.awt.Dimension(85, 23));
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });

        jButtonSearch1.setText("Search");
        jButtonSearch1.setMaximumSize(new java.awt.Dimension(85, 23));
        jButtonSearch1.setMinimumSize(new java.awt.Dimension(85, 23));
        jButtonSearch1.setPreferredSize(new java.awt.Dimension(85, 23));
        jButtonSearch1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearch1ActionPerformed(evt);
            }
        });

        jButtonSearch2.setText("Clear");
        jButtonSearch2.setMaximumSize(new java.awt.Dimension(85, 23));
        jButtonSearch2.setMinimumSize(new java.awt.Dimension(85, 23));
        jButtonSearch2.setPreferredSize(new java.awt.Dimension(85, 23));
        jButtonSearch2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearch2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jID, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jName)
                    .addComponent(jAddress)
                    .addComponent(jCountry)
                    .addComponent(jEmail)
                    .addComponent(jPhone))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextPhone, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                    .addComponent(jTextAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                    .addComponent(jTextName, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                    .addComponent(jTextID, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                    .addComponent(jTextCountry, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                    .addComponent(jTextEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonSearch1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonInsert)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonSearch2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(63, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jID)
                    .addComponent(jTextID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonSearch1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jName)
                    .addComponent(jTextName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jAddress)
                    .addComponent(jTextAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCountry)
                    .addComponent(jTextCountry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPhone)
                    .addComponent(jTextPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jEmail)
                    .addComponent(jTextEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonInsert)
                    .addComponent(jButtonUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonSearch2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextIDActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextIDActionPerformed

    private void jButtonUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUpdateActionPerformed
        CtrSupplier ctrS = new CtrSupplier();
	int id = Integer.parseInt(jTextID.getText());
        String name = jTextName.getText();
	String address = jTextAddress.getText();
	String country = jTextCountry.getText();
	String phone = jTextPhone.getText();
        String email = jTextEmail.getText();
        ctrS.updateSupplier(id, name, address, country, phone, email);
    }//GEN-LAST:event_jButtonUpdateActionPerformed

    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
        CtrSupplier ctrS = new CtrSupplier();
        String name = jTextName.getText();
        ModelLayer.Supplier sup = ctrS.findByName(name);
        putValuesOnTheScreen(sup);

    }//GEN-LAST:event_jButtonSearchActionPerformed

     private void putValuesOnTheScreen(ModelLayer.Supplier supObj)
    {
                jTextID.setText(Integer.toString(supObj.getId()));
                jTextName.setText(supObj.getName());
                jTextAddress.setText(supObj.getAddress());
                jTextCountry.setText(supObj.getCountry());
                jTextPhone.setText(supObj.getPhoneno());
                jTextEmail.setText(supObj.getEmail());
    }


    private void jButtonInsertActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonInsertActionPerformed
    {//GEN-HEADEREND:event_jButtonInsertActionPerformed
        CtrSupplier ctrS = new CtrSupplier();
        String name = jTextName.getText();
	String address = jTextAddress.getText();
	String country = jTextCountry.getText();
	String phone = jTextPhone.getText();
        String email = jTextEmail.getText();
        ctrS.insertNewSupplier(name, address, country, phone, email);

    }//GEN-LAST:event_jButtonInsertActionPerformed

    private void jButtonSearch1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonSearch1ActionPerformed
    {//GEN-HEADEREND:event_jButtonSearch1ActionPerformed
        CtrSupplier ctrS = new CtrSupplier();
        ModelLayer.Supplier sup = ctrS.findById(jTextID.getText());
        putValuesOnTheScreen(sup);
    }//GEN-LAST:event_jButtonSearch1ActionPerformed

    private void jButtonSearch2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonSearch2ActionPerformed
    {//GEN-HEADEREND:event_jButtonSearch2ActionPerformed
                jTextID.setText("");
                jTextName.setText("");
                jTextAddress.setText("");
                jTextCountry.setText("");
                jTextPhone.setText("");
                jTextEmail.setText("");
    }//GEN-LAST:event_jButtonSearch2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Supplier().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jAddress;
    private javax.swing.JButton jButtonInsert;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JButton jButtonSearch1;
    private javax.swing.JButton jButtonSearch2;
    private javax.swing.JButton jButtonUpdate;
    private javax.swing.JLabel jCountry;
    private javax.swing.JLabel jEmail;
    private javax.swing.JLabel jID;
    private javax.swing.JLabel jName;
    private javax.swing.JLabel jPhone;
    private javax.swing.JTextField jTextAddress;
    private javax.swing.JTextField jTextCountry;
    private javax.swing.JTextField jTextEmail;
    private javax.swing.JTextField jTextID;
    private javax.swing.JTextField jTextName;
    private javax.swing.JTextField jTextPhone;
    // End of variables declaration//GEN-END:variables

}
